<?php
// /web/controller/controller.php

// REQUIRE:
if(version_compare(PHP_VERSION, '5.3.0', '>=')) { require_once __DIR__.'/../include.inc'; } 
else { require_once dirname(__FILE__).'/../include.inc'; }

class Controller
{	
	public $routes;
	
	public function __construct() {
		// Route collection
		$this 
			// Basic:
			->add_route(Route::INDEX , false, 'Welcome', 'main.twig')
			->add_route(Route::LOGIN, false, 'Login', 'login.twig')
			->add_route(Route::LOGOUT, false, 'Logout', null)
			->add_route(Route::SEARCH_SUB, true, 'Search', 'search_list.twig')
			->add_route(Route::SEARCH, false, 'Search', 'search.twig')
			->add_route(Route::SITEMAP , false, 'Sitemap', 'sitemap.twig')
			->add_route(Route::FAQ, false, 'F.A.Q - Frequently Asked Questions', 'faq.twig')
			->add_route(Route::ABOUT , false, 'About', 'about.twig')
			
			// Account:
			->add_route(Route::ACCOUNT , false, 'Account', 'account.twig')
			->add_route(Route::ACCOUNT_CREATE, false, 'Create an Account', 'account_create.twig')
			->add_route(Route::ACCOUNT_PANEL, false, 'My Account', 'account_panel.twig')
			->add_route(Route::ACCOUNT_PANEL_ACTIONS, true, 'Account - ', null)
			->add_route(Route::ACCOUNT_VIEW , true, 'Account', 'account_view.twig')
			->add_route(Route::ACCOUNT_LIST, false, 'Account list', 'account_list')

			// Admin:
			->add_route(Route::ADMIN_PANEL , false, 'Admin Panel', 'admin_panel.twig')
			->add_route(Route::ADMIN_PANEL_ACTIONS , true, 'Admin -', null)
			
			// Posts:
			->add_route(Route::POST_LIST, false, 'Posts', 'post_list.twig')
			->add_route(Route::POST_ACTIONS, true, '', null)
			->add_route(Route::POST_VIEW , true, 'Post - ', 'post_ext.twig')
			->add_route(Route::POST_WRITE, false, 'Write an article', 'post_write.twig')
			->add_route(Route::POST_EDIT, true, 'Edit Article - ', 'post_edit.twig');
	}
		
	public function invoke($app)
	{
		
		try
		{
			if(!$app) 
				throw new Exception('Silex Provider not initialized.');
			
			$obj_model = new Model();		
			foreach($this->routes as $route => $route_info)
			{
				if($route_info['scope'])
				{
					$app->get($route, function($sub) use($app, $route_info) {
						$render = null;
						switch($route_info)
						{
							// This switch logic contains routes with variable scopes.
							// Basic:
							case Route::SEARCH_SUB: { break; }
							
							// Account:
							case Route::ACCOUNT_PANEL_ACTIONS: { break; }
							case Route::ACCOUNT_VIEW: { break; }
							
							// Admin:
							case Route::ADMIN_PANEL_ACTIONS: { break; }
							
							// Post:
							case Route::POST_ACTIONS: { break; }
							case Route::POST_VIEW: { break; }
							case Route::POST_EDIT: { break; }
						
							// Default:
							default: { throw new Exception('Oops.. This page is not available.'); break; }
						}
						return $route_info['title'].' '.$sub;			
					});
				} else {
					$app->get($route, function() use($app, $route_info, $obj_model) {
						$render = null;
						switch($route_info['route'])
						{
							// See Route class for the constant definitions.
							// Basic:
							case Route::INDEX: { break; }
							case Route::LOGIN: { break; }
							case Route::LOGOUT: { break; }
							case Route::SITEMAP: { break; }
							case Route::FAQ: { break; }
							case Route::SEARCH; { break; }
							case Route::ABOUT: { break; }
							
							// Account:
							case Route::ACCOUNT: { break; }
							case Route::ACCOUNT_CREATE: { break; }
							case Route::ACCOUNT_PANEL: { break; }
							case Route::ACCOUNT_LIST: { break; }
							
							// Admin:
							case Route::ADMIN_PANEL: { break; }
							
							
							// Posts:
								// Views a list of posts.
							case Route::POST_LIST: { 
								$obj_posts = new M_Posts();
								$post_list = $obj_posts->get_posts();
								$render = $app['twig']->render($route_info['view'], array(
									'title' => $route_info['title'],
									'posts' => $post_list,
								));
								break;
							}
							case Route::POST_WRITE: { break; }

							// Default:
							default: { throw new Exception('Oops.. this page is not available.'); break; }
						}
						if($render != null)
							return $render;
						else 
							throw new Exception('Oops.. this page is not available.');
					});
				}
			}
		}
		catch(Exception $e) {
			$message = $e->getMessage();
			return $app['twig']->render('error.twig', array('message' => $message));
		}
		
		// Register error handlers
		$app->error(
			function(\Exception $e) use ($app)
			{
				if ($e instanceof NotFoundHttpException) 
				{
					$content = $app['twig']->render('error.twig', array('code' => 404));
					return $content;
				}
				$code = ($e instanceof HttpException) ? $e->getStatusCode() : 500;
				$message = $e->getMessage();
				$content = $app['twig']->render('error.twig', array('code' => $code, 'message' => $message));
				return $content;
			}
		);
			
		$app->run();
	}
	
	public function add_route($_route, $_scope, $_title, $_template)
	{
		$obj_route = new Route($_route, $_scope, $_title, $_template);
		$this->routes[$obj_route->route] = array(
			'route' => $obj_route->route,
			'scope' => $obj_route->scope,
			'title' => $obj_route->title,
			'view'  => $obj_route->template
		 );
		 
		return $this;		
	}
}
?>